﻿#include "levelpin.h"

#include <QApplication>
#include <QFile>
#include <QDebug>


LevelPin  * LevelPin::m_levelPin = NULL;
LevelPin::LevelPin(QObject *parent) : QObject(parent)
{
    initPin();

}

LevelPin::~LevelPin()
{

}

void LevelPin::initPin()
{
    m_levelInfor.clear();
    QString docParhFile =  QApplication::applicationDirPath();
    docParhFile +="/configs/levelpins.json";

    QFile files(docParhFile);
    if(files.open(QIODevice::ReadOnly)){
        QString inforsa = files.readAll();
        QJsonParseError errors;
        QJsonDocument doc = QJsonDocument::fromJson(inforsa.toUtf8(),&errors);
#ifdef DEBUG_INFOR
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"doc=="<<doc;
        qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"errors.error=="<<errors.error;
#endif
        if(errors.error == QJsonParseError::NoError){
            QJsonArray arrya = doc.array();
            for(int i = 0 ; i < arrya.size() ;i++ ){
                QJsonObject itema = arrya.at(i).toObject();
#ifdef DEBUG_INFOR
                qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"itema=="<<itema;
#endif
                int indexs =  itema.take("pins").toInt();
                QJsonArray levels = itema.take("levels").toArray();
                QMap<int,int> mapa;
                for(int j = 0 ; j < levels.size() ;j++ ){
                    QJsonObject itemb = levels.at(j).toObject();
                    int measure = itemb.take("measure").toInt();
                    int reals = itemb.take("reals").toInt();
                    mapa.insert(measure,reals);
                }
                m_levelInfor.insert(indexs,mapa);
            }
        }
        files.close();
    }
#ifdef DEBUG_INFOR
    qDebug()<<__func__<<__LINE__;
#endif

}

LevelPin *LevelPin::gestance()
{
    if(m_levelPin == NULL){

        m_levelPin = new LevelPin();
    }

    return m_levelPin;

}

int LevelPin::getRealHeight(int pins, int heights)
{
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"pins=="<<pins<<"heights=="<<heights;
#endif
    QMap<int ,int> realHeightList;

    realHeightList  = m_levelInfor.value(pins,realHeightList);
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"realHeightList=="<<realHeightList;
#endif
    if(realHeightList.size() <= 0){
        return heights;
    }

    QList<int> listsa = realHeightList.keys();
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"listsa=="<<listsa;
#endif
    int lina = -1;
    int linb = -1;
    for(int i = 0 ; i < listsa.size() -1; i++){
        int linc =listsa.at(i);
        int lind =listsa.at(i+1);
        if(linc <= heights && heights <= lind){
            lina =linc;
            linb =lind;
            break;
        }
    }
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"lina=="<<lina<<"linb=="<<linb;
#endif
    if( lina <= 0 || linb <= 0 ){
        return heights;
    }
    int hina = realHeightList.value(lina,-1);
    int hinb = realHeightList.value(linb,-1);
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"hinb=="<<hinb<<"hina=="<<hina;
#endif
    if( hina <= 0 || hinb <= 0 ){
        return heights;
    }
    int hinc = heights - lina;
    int hind = hinb - hina;
    int hinf = linb - lina;
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"hinc=="<<hinc;
#endif
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"hind=="<<hind;
#endif
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"hinf=="<<hinf;
#endif
    if(hinf <= 0){
        hinf = 1;
    }
#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"hinf=="<<hinf;
#endif
    int hinh = hina + hinc * hind / hinf;

#ifdef DEBUG_INFOR
    qDebug()<< metaObject()->className()<<__func__<<__LINE__<<"hinh=="<<hinh;
#endif
    return hinh;
}
